/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.repository;

import me.zhengjie.business.domain.Jzdj;
import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.service.dto.YbryDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-04-15
**/
public interface YbryRepository extends JpaRepository<Zhsq, String>, JpaSpecificationExecutor<Zhsq> {

    /**
     * 查询人员基本信息
     * @param sfzh 身份证号
     * @return Map<String,Object>
     */
    @Query(value = "select " +
            /* 参保统筹区 */
            "info.AAB034_NAME,  " +
            /* 医保个人编码 */
            "info.AAC001,  " +
            /* 姓名 */
            "info.AAC003,  " +
            /* 证件号码 */
            "info.AAC002,  " +
            /* 参保类型 */
            "info.AAC008,  " +
            /* 人员类型 */
            "info.AKC021,  " +
            /* 社会保障卡号 */
            "info.CARDNO,  " +
            /* 参保状态 */
            "info.AAC031,  " +
            /* 参保统筹区代码 */
            "info.AAB034_CODE,  " +
            /* 参保类型代码 */
            "info.AAC008_CODE,  " +
            /* 人员类型代码 */
            "info.AKC021_CODE,  " +
            /* 参保状态代码 */
            "info.AAC031_CODE from view_person info where aac002 = ?1 and rownum=1",nativeQuery = true)
    Object queryCareinsuInfo(String sfzh);

    /**
     * 根据身份证查询是否在享受居家待遇
     * @param sfzh 身份证号
     * @return Map<String,Object>
     */
    @Query(value = "SELECT T.* FROM BIS_ZHSQ T WHERE " +
            "T.BIS_ZHSQ_SNRY_SFZ = ?1 AND " +
            "T.BIS_ZHSQ_ZHLB='03' AND " +
            "TO_CHAR(T.BIS_ZHSQ_DYKKRQ, 'YYYY-MM-DD') <= TO_CHAR(SYSDATE, 'YYYY-MM-DD') AND " +
            "TO_CHAR(T.BIS_ZHSQ_DYJSRQ, 'YYYY-MM-DD') >= TO_CHAR(SYSDATE, 'YYYY-MM-DD')", nativeQuery = true)
    List<Zhsq> querySnryHomeTreatment(String sfzh);

    /**
     * 根据身份证查询申请中的居家照护
     * @param sfzh 身份证号
     * @return Map<String,Object>
     */
    @Query(value = "SELECT * FROM BIS_ZHSQ T WHERE " +
            "T.BIS_ZHSQ_SNRY_SFZ=?1 AND " +
            "T.BIS_ZHSQ_ZHLB='03' AND " +
            "T.BIS_ZHSQ_DYKKRQ IS NULL AND " +
            "T.Bis_Zhsq_Dyjsrq IS NULL AND " +
            "T.BIS_ZHSQ_ZT IN ('00','01','02','04','05','06','07','11','12')", nativeQuery = true)
    List<Zhsq> querySnryHomeApplying(String sfzh);

    /**
     * 根据身份证查询申请中的机构照护
     * @param sfzh 身份证号
     * @return Map<String,Object>
     */
    @Query(value = "SELECT * FROM BIS_ZHSQ T WHERE " +
            "T.BIS_ZHSQ_SNRY_SFZ=?1 AND " +
            "T.BIS_ZHSQ_ZHLB='01' AND " +
            "T.BIS_ZHSQ_ZT IN ('00','01','02','04','05','54','55','80')", nativeQuery = true)
    List<Zhsq> querySnryOrgApplying(String sfzh);
}